package com.example.demo.controller;

import com.example.demo.entity.Contract;
import com.example.demo.entity.Designer;
import com.example.demo.service.ContractService;
import com.example.demo.service.CustomerService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/contract")
public class ContractController {
    @Resource
    private ContractService contractService;
    @Resource
    private CustomerService customerService;

    @RequestMapping(value = "",method = RequestMethod.GET)
    private String contract(HttpSession session){
        Designer designer =(Designer) (session.getAttribute("designer"));
        if(designer==null){
            return "login";
        }
        return "contractList";
    }

    @RequestMapping(value = "contractAdd",method = RequestMethod.GET)
    private String contractAdd(HttpSession session){
        Designer designer =(Designer) (session.getAttribute("designer"));
        if(designer==null){
            return "login";
        }
        return "contractAdd";
    }

    @RequestMapping(value = "/contractList",method = RequestMethod.POST)
    @ResponseBody
    private Map contractList(HttpSession session){
        Map map = new HashMap();
        Designer designer =(Designer) (session.getAttribute("designer"));
        String desId = designer.getDesId();
        List<Contract> contractList = contractService.getAllContractByDesId(desId);
        if(contractList==null || contractList.size()==0){
            map.put("msg",0);
        } else {
            map.put("msg",1);
            map.put("contractList",contractList);
        }
        return map;
    }

    @RequestMapping(value = "/contractAdd",method = RequestMethod.POST)
    @ResponseBody
    private Integer contractAdd(Contract contract){
        Integer rel = contractService.addContract(contract);
        if(rel>0){
            //签约成功后,修改客户表的状态为已签约
            customerService.updateIsContract(contract.getUserId());
        }
        return rel;
    }

    @RequestMapping(value = "/contractDetail/{conCode}",method = RequestMethod.GET)
    private String contractDetail(@PathVariable(name = "conCode")String conCode,HttpSession session){
        Designer designer =(Designer) (session.getAttribute("designer"));
        if(designer==null){
            return "login";
        } else {
            Contract contract = contractService.getContractByCode(conCode);
            session.setAttribute("contract",contract);
            return "contractDetail";
        }
    }

    @RequestMapping(value = "/contractDetail",method = RequestMethod.GET)
    @ResponseBody
    private Contract contractDetail(HttpSession session){
        Contract contract =(Contract) (session.getAttribute("contract"));
        return contract;
    }

    @RequestMapping(value = "/contractModify",method = RequestMethod.POST)
    @ResponseBody
    private Integer contractModify(Contract contract){
        Integer rel = contractService.modifyContractByCode(contract);
        return rel;
    }

    @RequestMapping(value = "/getContractByCode",method = RequestMethod.POST)
    @ResponseBody
    private Map getContractByCode(String conCode){
        Map map = new HashMap();
        Contract contract = contractService.getContractByCode(conCode);
        if(contract==null){
            map.put("msg",0);
        } else {
            map.put("msg",1);
            map.put("contract",contract);
        }
        return map;
    }

    @RequestMapping("/mysearch")
    @ResponseBody
    public List<Contract> mysearch(String keyword,HttpSession session){
        Designer designer =(Designer) (session.getAttribute("designer"));
        List<Contract> contractList = contractService.getContract(keyword,designer.getDesId());
        return contractList;
    }
}
